import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * user:likai
 * Date:2022-12-25
 * Time:8:42
 */
@WebServlet("/login")
public class LoginServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username=req.getParameter("username");
        String password=req.getParameter("password");
        if("zhangsan".equals(username)&&"123".equals(password)){
            //创建会话
            //创建一个sessionId和HttpSession对象
            //然后把这两个以键值对的方式存储起来，存放到哈希表里
            //然后把sessionId通过set—Cookie写回浏览器中
            HttpSession session=req.getSession(true);
            //该会话绑定一个对象
            session.setAttribute("username","zhangsan");
            //重定向
            resp.sendRedirect("index");
        }else{
            resp.setStatus(403);
            resp.setContentType("text/html;charset=utf8");
            resp.getWriter().write("登陆失败，用户名或者密码错误");
        }
    }
}
